angular.module('mamApp').controller('albumManageController', ['$scope', 'AlbumManager', '$state', 'api', 'FileUploader', 'ImsDialog', 'CommonManager', '$filter','BaseValidations','$uibModal','ImsErr',
    function ($scope, AlbumManager, $state, api, FileUploader, ImsDialog, CommonManager, $filter,BaseValidations,$uibModal,ImsErr) {


        $scope.failReason = [
            {key: 204, value: $scope.table[204]},
            {key: 210, value: $scope.table[210]},
            {key: 211, value: $scope.table[211]},
            {key: 212, value: $scope.table[212]}
        ];

        $scope.reviewError={};
        $scope.BaseValidations = BaseValidations;
        var albumId = $state.params.albumId;
        $scope.statusChange =function(){
            $scope.formData.reviewErrorId = $scope.formData.reviewErrorId ? $scope.formData.reviewErrorId : 204;
        }

        AlbumManager.albumDetail(albumId).then(function (result) {
            $scope.formData = result.data.adminAlbumVO;
            $scope.formData.keyword = $scope.formData.keyword.join(",");

            $scope.reviewError=$filter('filter')( $scope.failReason,{key: $scope.formData.reviewErrorId})[0];
            var tmpComm = [];
            var tmpSpe = [];
            $scope.formData.commList.forEach(function (tag) {
                tmpComm.push({
                    commtagId: tag.commtagId,
                    dimensionId: tag.dimensionId,
                    commtagName: tag.commtagName
                })
            })
            $scope.formData.specialList.forEach(function (tag) {
                tmpSpe.push({
                    specialId: tag.specialId,
                    dimensionId: tag.dimensionId,
                    specialName: tag.specialName
                })
            })
            $scope.commonTagsSelected = tmpComm;
            $scope.specialTagsSelected = tmpSpe;

            $scope.specialTagsList($scope.formData.channelId)
            CommonManager.categoriesList().then(function (result) {
                $scope.categories = result.data.pageList;
                $scope.categorySelected = $filter('filter')($scope.categories, {categoryId: $scope.formData.categoryId})[0];
                $scope.categoryChange($scope.categorySelected);

            })


        })


        $scope.categoryChange = function (category) {
            $scope.formData.categoryId=category.categoryId;
            CommonManager.channelList(category.categoryId).then(function (result) {
                $scope.channels = result.data.pageList;

                $scope.channelSelected = $filter('filter')($scope.channels, {channelId: $scope.formData.channelId})[0];

            })
        }
        CommonManager.commonTagsList().then(function (result) {
            $scope.commonTags = [];
            $scope.commonDimensionsName = {};
            result.data.pageList.forEach(function (item) {
                $scope.commonDimensionsName[item.dimensionId] = item.dimensionsName;
                if (item.hasOwnProperty("list")) {
                    item.list.forEach(function (tag) {
                        $scope.commonTags.push({
                            commtagId: tag.commtagId,
                            dimensionId: item.dimensionId,
                            commtagName: tag.commtagName
                        })
                    })
                }
            })
        })


        $scope.channelChange = function (channel) {
            $scope.specialTagsSelected = [];
            $scope.formData.channelId = channel.channelId;
            $scope.specialTagsList(channel.channelId);

        }
        $scope.specialTagsList = function (channelId) {
            $scope.specialTags = [];
            $scope.specialDimensionsName = {};

            CommonManager.specialTagsList(channelId).then(function (result) {
                if (result.data.pageList) {
                    result.data.pageList.forEach(function (item) {
                        $scope.specialDimensionsName[item.dimensionId] = item.dimensionsName;

                        if (item.hasOwnProperty("list")) {
                            item.list.forEach(function (tag) {
                                $scope.specialTags.push({
                                    specialId: tag.specialId,
                                    dimensionId: tag.dimensionId,
                                    specialName: tag.specialName
                                })
                            })
                        }

                    })
                }


            })
        }


        /**
         * 通用标签分组
         * @param item
         * @returns {*}
         */

        $scope.commGroupFn = function (item) {
            return $scope.commonDimensionsName[item.dimensionId]
        };
        /**
         * 通用标签分组
         * @param item
         * @returns {*}
         */

        $scope.specialGroupFn = function (item) {
            return $scope.specialDimensionsName[item.dimensionId]
        };



        var handleFileSelect = function (evt) {
            var file = evt.currentTarget.files[0];
            if(file.type.indexOf("image")==-1){
                $scope.$apply($scope.errorImg='仅支持PNG/JPG/JPEG格式');
                return false;
            }else {
                $scope.$apply($scope.errorImg='');
            }

            $scope.uploadCover(file)
        };

        setTimeout(function () {
            angular.element(document.querySelector('#fileInput')).on('change', handleFileSelect);
        }, 1000)

        $scope.uploadCover = function (image) {
            if(image.type.indexOf("image")==-1){
                $scope.$apply($scope.errorImg='仅支持PNG/JPG/JPEG格式');
                return false;
            }else {
                $scope.$apply($scope.errorImg='');
            }
            var modalInstance = $uibModal.open({
                animation: true,
                backdrop:false,
                templateUrl: 'uploadCover.html',
                controller: 'coverManageController',
                resolve: {
                    params: function () {
                        return {image: image,coverType:'album',cpId:$scope.formData.cpId};
                    }
                }
            });
            modalInstance.result.then(function (result) {
                $("#fileInput").prop("value","");
                if (result.url) {
                    $scope.formData.albumCover = result.url;

                }

            })
        }

        $scope.reviewErrorChange=function (reviewError) {
            $scope.formData.reviewErrorId=reviewError.key;
        }



        $scope.submit = function () {
            if ($scope.formData.reviewStatus != 9) {
                $scope.formData.reviewErrorMemo = "";
            }
            var commTag = [];
            var specialTag = [];
            $scope.commonTagsSelected.forEach(function (item) {
                commTag.push(item.commtagId)
            })
            $scope.specialTagsSelected.forEach(function (item) {
                specialTag.push(item.specialId)
            })
            $scope.formData.commTag = commTag.join(",")
            $scope.formData.specialTag = specialTag.join(",")
            ImsDialog.open({title: "专辑审核", message: "确定提交专辑资料以及认证信息？"}).then(function (a) {
                if (a == "ok") {
                    delete $scope.formData.duration;
                    $scope.formData.keyword = $scope.formData.keyword ? $scope.formData.keyword.split(",") : []
                    AlbumManager.albumEdit(albumId, $scope.formData).then(function (result) {
                        if (result.data.c == "s") {
                            $state.go("main.album", {}, {reload: true})
                        }else {
                            ImsErr.open({title: "提示", message: result.data.i}).then(function (a) {
                                $state.reload()
                            })
                        }
                    })
                }
            })

        }


    }])

